import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 前序排列
 * User: wangxin
 * Date: 2024-11-05
 * Time: 22:23
 */


   class TreeNode {

    int val = 0;

   TreeNode left = null;

    TreeNode right = null;


    public TreeNode(int val) {
 this.val = val;
    }
}

public class test1 {
    public void func(TreeNode root, List<Integer> list){
        if(root == null){
            return ;
        }
        list.add(root.val);
        func(root.left,list);
        func(root.right,list);
    }
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        func(root,list);
        int j = list.size();
        int[] arr = new int[j];
        for(int i = 0; i < j;i++){
            arr[i] = list.get(i);
        }
        return arr;

    }
}
